﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using FU.Capstones.IMS.Services.Common;
using FU.Capstones.IMS.Services.Common.DataContracts;
using FU.Capstones.IMS.Services.Common.Respondents;
using FU.Capstones.IMS.Services.DataContracts;
using FU.Capstones.IMS.Services.Interfaces;

namespace FU.Capstones.IMS.Services.DAOs
{
    internal class OrderDAO : DAOBase, IOrderDAO
    {
        #region IOrderDAO Members

        public CollectionFeedback<Order> GetCollection(bool isShowAll)
        {
            var myFeedBack = new CollectionFeedback<Order>();
            try
            {
                var command = new SqlCommand("GetCollectionOrder", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@isShowAll", isShowAll);
                var dt = new DataTable("AllOrder");
                var sqlda = new SqlDataAdapter(command);
                sqlda.Fill(dt);
                var listOrder = new List<Order>();
                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        var employeeA = new Employee();
                        if (dataRow["EmployeeAID"].GetType().Equals(typeof (int)))
                        {
                            employeeA.EmployeeID = Convert.ToInt32(dataRow["EmployeeAID"]);
                            if (dataRow["EmployeeAName"].GetType().Equals(typeof (string)))
                            {
                                employeeA.FullName = Convert.ToString(dataRow["EmployeeAName"]);
                            }
                        }

                        var employeeB = new Employee();
                        if (dataRow["EmployeeBID"].GetType().Equals(typeof (int)))
                        {
                            employeeB.EmployeeID = Convert.ToInt32(dataRow["EmployeeBID"]);
                            if (dataRow["EmployeeBName"].GetType().Equals(typeof (string)))
                            {
                                employeeB.FullName = Convert.ToString(dataRow["EmployeeBName"]);
                            }
                        }
                        var contact = new Contact();
                        if (dataRow["ContactID"].GetType().Equals(typeof (int)))
                        {
                            contact.ContactID = Convert.ToInt32(dataRow["ContactID"]);
                            if (dataRow["ContactName"].GetType().Equals(typeof (string)))
                            {
                                contact.FullName = Convert.ToString(dataRow["ContactName"]);
                            }
                        }
                        //this code to do for material order
                        var newOrder = new Order();
                        if (dataRow["OrderID"].GetType().Equals(typeof (int)))
                        {
                            newOrder.OrderID = Convert.ToInt32(dataRow["OrderID"]);
                        }

                        newOrder.OrderDate = Convert.ToDateTime(dataRow["OrderDate"]);
                        newOrder.OrderCode = Convert.ToString(dataRow["OrderCode"]);
                        newOrder.IsImported = Convert.ToBoolean(dataRow["IsImport"]);
                        if (dataRow["IsDelete"] != DBNull.Value)
                        {
                            newOrder.IsDeleted = Convert.ToBoolean(dataRow["IsDelete"]);
                        }
                        newOrder.EmployeeA = employeeA;
                        newOrder.EmployeeB = employeeB;
                        newOrder.Contact = contact;
                        newOrder.MaterialCollection = null;
                        newOrder.ProductCollection = null;

                        listOrder.Add(newOrder);
                    }
                    myFeedBack.Message = InventoryConstants.Messages.GetSuccessfully;
                    myFeedBack.Data = listOrder;
                    myFeedBack.IsSuccessful = true;
                }
                else
                {
                    myFeedBack.Message = InventoryConstants.Messages.NA;
                    myFeedBack.Data = new List<Order>();
                    myFeedBack.IsSuccessful = true;
                }
                //Log.Info("Get All Order: {0}, {1}", myFeedBack.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                myFeedBack.Data = null;
                myFeedBack.IsSuccessful = false;
            }
            catch (Exception e)
            {
                //Log.Error(e.Message);
                myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                myFeedBack.Data = null;
                myFeedBack.IsSuccessful = false;
            }
            finally
            {
                CloseConnection();
            }
            return myFeedBack;
        }

        public CollectionFeedback<Order> GetCollectionType(bool isShowAll)
        {
            var myFeedBack = new CollectionFeedback<Order>();
            try
            {
                var command = new SqlCommand("GetCollectionOrder", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                var dt = new DataTable("AllOrder");
                var sqlda = new SqlDataAdapter(command);
                sqlda.Fill(dt);
                var listOrder = new List<Order>();

                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        int orderId = Convert.ToInt32(dataRow["OrderID"]);
                        string orderCode = Convert.ToString(dataRow["OrderCode"]);
                        DateTime orderDate = Convert.ToDateTime(dataRow["OrderDate"]);
                        int employeeAid = Convert.ToInt32(dataRow["EmployeeAID"]);
                        int contactId = Convert.ToInt32(dataRow["ContactID"]);
                        int employeeBid = Convert.ToInt32(dataRow["EmployeeBID"]);
                        Boolean isImport = Convert.ToBoolean(dataRow["isImport"]);


                        //this is material order
                        //this code to do for material order
                        var newOrder = new Order();
                        newOrder.OrderID = orderId;
                        newOrder.OrderDate = orderDate;
                        newOrder.OrderCode = orderCode;
                        newOrder.IsImported = isImport;

                        #region EmployeeA

                        if (employeeAid != 0)
                        {
                            var employeeCommand = new SqlCommand("GetSingleEmployee", imsConnection);
                            employeeCommand.CommandType = CommandType.StoredProcedure;
                            employeeCommand.Parameters.AddWithValue("@EmployeeID", employeeAid);
                            OpenConnection();

                            SqlDataReader reader = employeeCommand.ExecuteReader();

                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    var newEmployee = new Employee();

                                    newEmployee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
                                    newEmployee.FullName = Convert.ToString(reader["FullName"]);
                                    newEmployee.DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]);
                                    newEmployee.IdentityCardNumber = Convert.ToString(reader["IdentityCardNumber"]);
                                    newEmployee.Email = Convert.ToString(reader["Email"]);
                                    newEmployee.Gender = Convert.ToString(reader["Gender"]);
                                    newEmployee.JoinedDate = Convert.ToDateTime(reader["JoinedDate"]);
                                    newEmployee.Address = Convert.ToString(reader["Address"]);
                                    newEmployee.PhoneNumber = Convert.ToString(reader["PhoneNumber"]);
                                    newEmployee.MobileNumber = Convert.ToString(reader["MobileNumber"]);
                                    newEmployee.Description = Convert.ToString(reader["Description"]);

                                    //create a new employmentType
                                    var newEmploymentType = new EmploymentType();
                                    newEmploymentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
                                    newEmploymentType.EmploymentTypeName = Convert.ToString(reader["[EmploymentType]"]);

                                    //create a new role
                                    var newRole = new Role();
                                    newRole.RoleID = Convert.ToInt32(reader["RoleID"]);
                                    newRole.RoleName = Convert.ToString(reader["RoleName"]);

                                    newEmployee.EmploymentType = newEmploymentType;
                                    newEmployee.Role = newRole;

                                    newOrder.EmployeeA = newEmployee;
                                    break;
                                }
                            }
                            else
                            {
                                newOrder.EmployeeA = null;
                            }
                        }
                        else
                        {
                            newOrder.EmployeeA = null;
                        }

                        #endregion

                        //this block code use to set employeeB to neworder

                        #region EmployeeB

                        if (employeeBid != 0)
                        {
                            var employeeCommand = new SqlCommand("GetSingleEmployee", imsConnection);
                            employeeCommand.CommandType = CommandType.StoredProcedure;
                            employeeCommand.Parameters.AddWithValue("@EmployeeID", employeeBid);
                            OpenConnection();

                            SqlDataReader reader = employeeCommand.ExecuteReader();

                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    var newEmployee = new Employee();

                                    newEmployee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
                                    newEmployee.FullName = Convert.ToString(reader["FullName"]);
                                    newEmployee.DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]);
                                    newEmployee.IdentityCardNumber = Convert.ToString(reader["IdentityCardNumber"]);
                                    newEmployee.Email = Convert.ToString(reader["Email"]);
                                    newEmployee.Gender = Convert.ToString(reader["Gender"]);
                                    newEmployee.JoinedDate = Convert.ToDateTime(reader["JoinedDate"]);
                                    newEmployee.Address = Convert.ToString(reader["Address"]);
                                    newEmployee.PhoneNumber = Convert.ToString(reader["PhoneNumber"]);
                                    newEmployee.MobileNumber = Convert.ToString(reader["MobileNumber"]);
                                    newEmployee.Description = Convert.ToString(reader["Description"]);

                                    //create a new employmentType
                                    var newEmploymentType = new EmploymentType();
                                    newEmploymentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
                                    newEmploymentType.EmploymentTypeName = Convert.ToString(reader["[EmploymentType]"]);

                                    //create a new role
                                    var newRole = new Role();
                                    newRole.RoleID = Convert.ToInt32(reader["RoleID"]);
                                    newRole.RoleName = Convert.ToString(reader["RoleName"]);

                                    newEmployee.EmploymentType = newEmploymentType;
                                    newEmployee.Role = newRole;

                                    newOrder.EmployeeB = newEmployee;
                                    break;
                                }
                            }
                            else
                            {
                                newOrder.EmployeeB = null;
                            }
                        }
                        else
                        {
                            newOrder.EmployeeB = null;
                        }

                        #endregion

                        //this block code use to set contact

                        #region Contact

                        if (contactId != 0)
                        {
                            var contactCommand = new SqlCommand("GetSingleContact", imsConnection);
                            contactCommand.CommandType = CommandType.StoredProcedure;
                            contactCommand.Parameters.AddWithValue("@ContactID", contactId);
                            OpenConnection();

                            SqlDataReader reader = contactCommand.ExecuteReader();

                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    var newContact = new Contact();
                                    newContact.ContactID = Convert.ToInt32(reader["ContactID"]);
                                    newContact.FullName = Convert.ToString(reader["FullName"]);
                                    newContact.MobilePhone = Convert.ToString(reader["MobilePhone"]);
                                    newContact.PartnerID = 0;
                                    newOrder.Contact = newContact;
                                    break;
                                }
                            }
                            else
                            {
                                newOrder.Contact = null;
                            }
                        }
                        else
                        {
                            newOrder.Contact = null;
                        }

                        #endregion

                        if (orderCode.Contains("M"))
                        {
                            //this block code use to set material list

                            #region Material List

                            var materialCommand = new SqlCommand("GetGroupOfMaterial", imsConnection);
                            materialCommand.CommandType = CommandType.StoredProcedure;
                            materialCommand.Parameters.AddWithValue("@orderID", orderId);
                            OpenConnection();
                            SqlDataReader myReader = materialCommand.ExecuteReader();

                            if (myReader.HasRows)
                            {
                                var lisMaterial = new ObservableCollection<Material>();
                                while (myReader.Read())
                                {
                                    var newMaterial = new Material();
                                    newMaterial.MaterialID = Convert.ToInt32(myReader["MaterialID"]);
                                    newMaterial.MaterialCode = Convert.ToString(myReader["MaterialCode"]);
                                    newMaterial.MaterialName = Convert.ToString(myReader["MaterialName"]);
                                    newMaterial.Description = Convert.ToString(myReader["Description"]);
                                    newMaterial.Quantity = Convert.ToInt32(myReader["Quantity"]);
                                    newMaterial.PricePerUnit = Convert.ToDouble(myReader["PricePerUnit"]);
                                    lisMaterial.Add(newMaterial);
                                }
                                newOrder.MaterialCollection = lisMaterial;
                            }
                            else
                            {
                                newOrder.MaterialCollection = null;
                            }

                            #endregion
                        }
                        else
                        {
                            //this block code use to set product list

                            #region product List

                            var productCommand = new SqlCommand("GetGroupOfProduct", imsConnection);
                            productCommand.CommandType = CommandType.StoredProcedure;
                            productCommand.Parameters.AddWithValue("@orderID", orderId);
                            OpenConnection();
                            SqlDataReader myReader = productCommand.ExecuteReader();

                            if (myReader.HasRows)
                            {
                                var listProduct = new ObservableCollection<Product>();
                                while (myReader.Read())
                                {
                                    var newProduct = new Product();
                                    newProduct.ProductID = Convert.ToInt32(myReader["ProductID"]);
                                    newProduct.ProductCode = Convert.ToString(myReader["ProductCode"]);
                                    newProduct.ProductName = Convert.ToString(myReader["ProductName"]);
                                    newProduct.Description = Convert.ToString(myReader["Description"]);
                                    newProduct.Quantity = Convert.ToInt32(myReader["Quantity"]);
                                    newProduct.MaterialCollection = null;
                                    newProduct.PricePerUnit = 0;
                                    listProduct.Add(newProduct);
                                }
                                newOrder.ProductCollection = listProduct;
                            }
                            else
                            {
                                newOrder.ProductCollection = null;
                            }

                            #endregion
                        }


                        listOrder.Add(newOrder);
                    }
                    myFeedBack.Message = InventoryConstants.Messages.GetSuccessfully;
                    myFeedBack.Data = listOrder;
                    myFeedBack.IsSuccessful = true;
                }
                else
                {
                    myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                    myFeedBack.Data = null;
                    myFeedBack.IsSuccessful = false;
                }
                //Log.Info("Get All Order: {0}, {1}", myFeedBack.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                myFeedBack.Data = null;
                myFeedBack.IsSuccessful = false;
            }
            catch (Exception e)
            {
                //Log.Error(e.Message);
                myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                myFeedBack.Data = null;
                myFeedBack.IsSuccessful = false;
            }
            finally
            {
                CloseConnection();
            }
            return myFeedBack;
        }

        public CollectionFeedback<Order> GetOrderCollectionByParameter(bool? isMaterial)
        {
            return getAllOrder("GetCollectionOrder", true, isMaterial);
        }

        public SingleFeedback<Order> GetSingle(int objectID)
        {
            var myFeedBack = new SingleFeedback<Order>();
            try
            {
                var cmGetOrderInfo = new SqlCommand("GetSingleOrderInfo", imsConnection);
                cmGetOrderInfo.CommandType = CommandType.StoredProcedure;
                cmGetOrderInfo.Parameters.AddWithValue("@OrderID", objectID);
                var dtOrderInfo = new DataTable("SingleOrderInfo");
                var sdaOrderInfo = new SqlDataAdapter(cmGetOrderInfo);

                sdaOrderInfo.Fill(dtOrderInfo);
                if (dtOrderInfo.Rows.Count != 0)
                {
                    var returedOrder = new Order();
                    foreach (DataRow dataRow in dtOrderInfo.Rows)
                    {
                        returedOrder.OrderID = Convert.ToInt32(dataRow["OrderID"]);
                        returedOrder.OrderCode = Convert.ToString(dataRow["OrderCode"]);
                        returedOrder.OrderDate = Convert.ToDateTime(dataRow["OrderDate"]);
                        returedOrder.IsImported = Convert.ToBoolean(dataRow["IsImport"]);
                        returedOrder.IsDeleted = Convert.ToBoolean(dataRow["IsDelete"]);
                        returedOrder.EmployeeA = new Employee();
                        returedOrder.EmployeeB = new Employee();
                        returedOrder.Contact = new Contact();
                        if (dataRow["EmployeeAName"] != DBNull.Value)
                        {
                            returedOrder.EmployeeA.EmployeeID = Convert.ToInt32(dataRow["EmployeeAID"]);
                            returedOrder.EmployeeA.FullName = Convert.ToString(dataRow["EmployeeAName"]);
                        }
                        if (dataRow["EmployeeBName"] != DBNull.Value)
                        {
                            returedOrder.EmployeeB.EmployeeID = Convert.ToInt32(dataRow["EmployeeBID"]);
                            returedOrder.EmployeeB.FullName = Convert.ToString(dataRow["EmployeeBName"]);
                        }
                        if (dataRow["ContactName"] != DBNull.Value)
                        {
                            returedOrder.Contact.ContactID = Convert.ToInt32(dataRow["ContactID"]);
                            returedOrder.Contact.FullName = Convert.ToString(dataRow["ContactName"]);
                        }
                    }
                    var cmGetSingleOrder = new SqlCommand("GetSingleOrder", imsConnection);
                    cmGetSingleOrder.CommandType = CommandType.StoredProcedure;
                    cmGetSingleOrder.Parameters.AddWithValue("@OrderID", objectID);
                    var dtSingleOrder = new DataTable("SingleOrderInfo");
                    var sdaSingleOrder = new SqlDataAdapter(cmGetSingleOrder);
                    sdaSingleOrder.Fill(dtSingleOrder);
                    if (dtSingleOrder.Rows.Count != 0)
                    {
                        var myMaterialCollection = new ObservableCollection<Material>();
                        foreach (DataRow dataRow in dtSingleOrder.Rows)
                        {
                            var myMaterial = new Material();
                            if (dataRow["MaMatHang"] != DBNull.Value)
                            {
                                myMaterial.MaterialCode = Convert.ToString(dataRow["MaMatHang"]);
                            }
                            if (dataRow["TenMatHang"] != DBNull.Value)
                            {
                                myMaterial.MaterialName = Convert.ToString(dataRow["TenMatHang"]);
                            }
                            if (dataRow["SoLuong"] != DBNull.Value)
                            {
                                myMaterial.Quantity = Convert.ToInt32(dataRow["SoLuong"]);
                            }
                            if (dataRow["GiaSanPham"] != DBNull.Value)
                            {
                                myMaterial.PricePerUnit = Convert.ToDouble(dataRow["GiaSanPham"]);
                            }

                            myMaterial.Warehouse = new Warehouse();
                            if (dataRow["KhoChua"] != DBNull.Value)
                            {
                                myMaterial.Warehouse.WarehouseID = Convert.ToInt32(dataRow["WarehouseID"]);
                                myMaterial.Warehouse.WarehouseName = Convert.ToString(dataRow["KhoChua"]);
                            }

                            myMaterialCollection.Add(myMaterial);
                        }
                        returedOrder.MaterialCollection = myMaterialCollection;
                    }

                    myFeedBack.Data = returedOrder;
                    myFeedBack.Message = InventoryConstants.Messages.GetSuccessfully;
                    myFeedBack.IsSuccessful = true;
                }
                else
                {
                    myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                    myFeedBack.Data = null;
                    myFeedBack.IsSuccessful = false;
                }


                //SqlCommand command = new SqlCommand("GetSingleOrder", imsConnection);
                //command.CommandType = CommandType.StoredProcedure;
                //command.Parameters.AddWithValue("@OrderID", objectID);
                //DataTable dt = new DataTable("AllOrder");
                //SqlDataAdapter sqlda = new SqlDataAdapter(command);
                //sqlda.Fill(dt);
                //if (dt.Rows.Count != 0)
                //{

                //    Order returedOrder = new Order();
                //    returedOrder.OrderID = objectID;
                //    ObservableCollection<Material> listMaterial = new ObservableCollection<Material>();
                //    foreach (DataRow dataRow in dt.Rows)
                //    {
                //        Material tempMaterial = new Material();
                //        tempMaterial.MaterialName = Convert.ToString(dataRow["TenMatHang"]);
                //        tempMaterial.Quantity = Convert.ToInt32(dataRow["SoLuong"]);

                //        tempMaterial.Warehouse = new Warehouse();
                //        tempMaterial.Warehouse.WarehouseName = Convert.ToString(dataRow["KhoChua"]);
                //        if (dataRow["GiaSanPham"].GetType().Equals(typeof(decimal)))
                //        {
                //            tempMaterial.PricePerUnit = Convert.ToDouble(dataRow["GiaSanPham"]);
                //        }
                //         listMaterial.Add(tempMaterial);

                //    }

                //    returedOrder.MaterialCollection = listMaterial;
                //    myFeedBack.Data = returedOrder;
                //    myFeedBack.Message = InventoryConstants.Messages.GetSuccessfully;
                //    myFeedBack.IsSuccessful = true;
                //}
                //else
                //{
                //    myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                //    myFeedBack.Data = null;
                //    myFeedBack.IsSuccessful = false;
                //}
                //Log.Info("Get single Order: {0}, {1}", myFeedBack.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                myFeedBack.Data = null;
                myFeedBack.IsSuccessful = false;
            }
            catch (Exception e)
            {
                //Log.Error(e.Message);
                myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                myFeedBack.Data = null;
                myFeedBack.IsSuccessful = false;
            }
            finally
            {
                CloseConnection();
            }
            return myFeedBack;
        }

        public SingleFeedback<int> Insert(Order data)
        {
            var feedback = new SingleFeedback<int> {IsSuccessful = false, Data = 0};

            try
            {
                var cmmInsertOrder = new SqlCommand("InsertOrder", imsConnection);
                cmmInsertOrder.CommandType = CommandType.StoredProcedure;
                cmmInsertOrder.Parameters.AddWithValue("@OrderCode", data.OrderCode);
                cmmInsertOrder.Parameters.AddWithValue("@OrderDate", Convert.ToDateTime(data.OrderDate));
                cmmInsertOrder.Parameters.AddWithValue("@EmployeeAID", data.EmployeeA.EmployeeID);
                if (data.Contact != null)
                {
                    if (data.Contact.ContactID != 0)
                    {
                        cmmInsertOrder.Parameters.AddWithValue("@ContactID", data.Contact.ContactID);
                    }
                }
                if (data.EmployeeB != null)
                {
                    if (data.EmployeeB.EmployeeID != 0)
                    {
                        cmmInsertOrder.Parameters.AddWithValue("@EmployeeBID", data.EmployeeB.EmployeeID);
                    }
                }

                cmmInsertOrder.Parameters.AddWithValue("@IsImport", Convert.ToBoolean(data.IsImported));
                cmmInsertOrder.Parameters.Add("@OrderID", SqlDbType.Int);
                cmmInsertOrder.Parameters["@OrderID"].Direction = ParameterDirection.Output;

                OpenConnection();
                cmmInsertOrder.ExecuteReader();
                int returedOrderId = Convert.ToInt32(cmmInsertOrder.Parameters["@OrderID"].Value);
                CloseConnection();

                if (returedOrderId < 1)
                {
                    feedback.Message = InventoryConstants.Messages.InsertFailed;
                    feedback.IsSuccessful = false;
                }
                else
                {
                    if (data.OrderCode.Contains("M"))
                    {
                        //insert list material into materialLoaf table
                        for (int i = 0; i < data.MaterialCollection.Count; i++)
                        {
                            Material tempMaterial = data.MaterialCollection[i];
                            var insertMaterial = new SqlCommand("InsertMaterialLoaf", imsConnection);

                            insertMaterial.CommandType = CommandType.StoredProcedure;
                            insertMaterial.Parameters.AddWithValue("@IsImport", Convert.ToBoolean(data.IsImported));
                            insertMaterial.Parameters.AddWithValue("@OrderID", returedOrderId);
                            insertMaterial.Parameters.AddWithValue("@MaterialID", tempMaterial.MaterialID);
                            insertMaterial.Parameters.AddWithValue("@WarehouseID", data.WarehouseID);
                            insertMaterial.Parameters.AddWithValue("@Quantity", tempMaterial.Quantity);
                            insertMaterial.Parameters.AddWithValue("@PricePerUnit",
                                                                   Convert.ToDouble(tempMaterial.PricePerUnit));

                            OpenConnection();
                            insertMaterial.ExecuteNonQuery();
                            CloseConnection();
                        }
                    }
                    else
                    {
                        //insert list product into productloaf talbe
                        for (int i = 0; i < data.ProductCollection.Count; i++)
                        {
                            Product tempProduct = data.ProductCollection[i];
                            var insertProduct = new SqlCommand("InsertProductLoaf", imsConnection);

                            insertProduct.CommandType = CommandType.StoredProcedure;
                            insertProduct.Parameters.AddWithValue("@IsImport", Convert.ToBoolean(data.IsImported));
                            insertProduct.Parameters.AddWithValue("@OrderID", returedOrderId);
                            insertProduct.Parameters.AddWithValue("@ProductID", tempProduct.ProductID);
                            insertProduct.Parameters.AddWithValue("@WarehouseID", data.WarehouseID);
                            insertProduct.Parameters.AddWithValue("@Quantity", tempProduct.Quantity);
                            insertProduct.Parameters.AddWithValue("@PricePerUnit", tempProduct.PricePerUnit);
                            OpenConnection();
                            insertProduct.ExecuteNonQuery();
                            CloseConnection();
                        }
                    }
                    feedback.Message = InventoryConstants.Messages.InsertSuccessfully;
                    feedback.IsSuccessful = true;
                    feedback.Data = returedOrderId;
                }

                //Log.Info("InsertAccount: {0}, {1}, {2}", data.EmployeeID, feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                //Log.Error(e.Message, null);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Update(Order data)
        {
            throw new NotImplementedException();
        }

        //public Feedback Delete(int objID)
        //{
        //    var feedback = new Feedback {IsSuccessful = false};

        //    try
        //    {
        //        OpenConnection();

        //        var command = new SqlCommand("DeleteOrder", imsConnection);

        //        command.CommandType = CommandType.StoredProcedure;
        //        command.Parameters.AddWithValue("@OrderID", objID);
        //        int rowAffect = command.ExecuteNonQuery();

        //        if (rowAffect >= 1)
        //        {
        //            feedback.Message = InventoryConstants.Messages.DeleteSuccessfully;
        //            feedback.IsSuccessful = true;
        //        }
        //        else
        //        {
        //            feedback.Message = InventoryConstants.Messages.DeleteFailed;
        //        }
        //    }
        //    catch (SqlException e)
        //    {
        //        feedback.Message = InventoryConstants.Messages.DeleteFailed;
        //    }
        //    catch (Exception t)
        //    {
        //        feedback.Message = InventoryConstants.Messages.DeleteFailed;
        //    }
        //    finally
        //    {
        //        CloseConnection();
        //    }
        //    return feedback;
        //}
        public Feedback Delete(int objID)
        {
            var feedback = new Feedback { IsSuccessful = false };

            try
            {
                OpenConnection();

                var command = new SqlCommand("DO_DeleteOrder", imsConnection);

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@OrderID", objID);

                command.Parameters.Add("@IsMaterial", SqlDbType.Int);
                command.Parameters["@IsMaterial"].Direction = ParameterDirection.Output;

                command.Parameters.Add("@IsImport", SqlDbType.Int);
                command.Parameters["@IsImport"].Direction = ParameterDirection.Output;

                command.Parameters.Add("@IsDeleted", SqlDbType.Int);
                command.Parameters["@IsDeleted"].Direction = ParameterDirection.Output;

                int rowAffect = command.ExecuteNonQuery();
                int isMaterial = Convert.ToInt32(command.Parameters["@IsMaterial"].Value);
                int isImport = Convert.ToInt32(command.Parameters["@IsImport"].Value);
                int isDeleted = Convert.ToInt32(command.Parameters["@IsDeleted"].Value);
                if (rowAffect >= 1)
                {
                    if (isDeleted == 0)
                    {
                        var cmdGetDeletedMaterial = new SqlCommand("[DO_GetDeletedMaterialOrder]", imsConnection);
                        cmdGetDeletedMaterial.CommandType = CommandType.StoredProcedure;
                        cmdGetDeletedMaterial.Parameters.AddWithValue("OrderID", objID);
                        var sqlda = new SqlDataAdapter(cmdGetDeletedMaterial);
                        var dt = new DataTable("AllDeletedMaterialAndProduct");
                        sqlda.Fill(dt);
                        var listMaterial = new List<Material>();
                        if (dt.Rows.Count != 0)
                        {
                            foreach (DataRow dataRow in dt.Rows)
                            {
                                Material myMaterial = new Material();
                                myMaterial.MaterialID = Convert.ToInt32(dataRow["MaterialID"]);
                                myMaterial.WarehouseId = Convert.ToInt32(dataRow["WarehouseID"]);
                                if (dataRow["Quantity"] != DBNull.Value)
                                {
                                    myMaterial.Quantity = Convert.ToInt32(dataRow["Quantity"]);
                                }
                                if (dataRow["PricePerUnit"] != DBNull.Value)
                                {
                                    myMaterial.PricePerUnit = Convert.ToDouble(dataRow["PricePerUnit"]);
                                }
                                listMaterial.Add(myMaterial);

                            }
                            string listMaterialOrProduct = GetMaterialLoafString(listMaterial);
                            var cmdUpdateWarehouseMaterial = new SqlCommand("[DO_UpdateWarehouseMaterial]", imsConnection);
                            cmdUpdateWarehouseMaterial.CommandType = CommandType.StoredProcedure;
                            cmdUpdateWarehouseMaterial.Parameters.AddWithValue("@IsMaterial", isMaterial);
                            cmdUpdateWarehouseMaterial.Parameters.AddWithValue("@IsImport", isImport);
                            cmdUpdateWarehouseMaterial.Parameters.AddWithValue("@ListMaterialString", listMaterialOrProduct);
                            int affectedRowUpdate = cmdUpdateWarehouseMaterial.ExecuteNonQuery();
                            if (affectedRowUpdate >= 1)
                            {
                                feedback.IsSuccessful = true;
                                feedback.Message = InventoryConstants.Messages.DeleteSuccessfully;
                            }
                            else
                            {
                                feedback.Message = InventoryConstants.Messages.DeleteFailed;
                            }

                        }
                        else
                        {
                            feedback.Message = InventoryConstants.Messages.DeleteFailed;
                        }
                    }
                    else
                    {
                        feedback.Message = "Đơn hàng này đã xóa rồi";
                    }
                }
            }
            catch (SqlException e)
            {
                feedback.Message = InventoryConstants.Messages.DeleteFailed;
            }
            catch (Exception t)
            {
                feedback.Message = InventoryConstants.Messages.DeleteFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }


        public CollectionFeedback<Order> Search(Order criteria)
        {
            var oCriteria = (OrderCriteria) criteria;
            if (oCriteria == null && criteria != null)
            {
                oCriteria = new OrderCriteria
                                {
                                    OrderCode = criteria.OrderCode
                                };
            }

            var feedback = new CollectionFeedback<Order> {IsSuccessful = false, Data = new List<Order>()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("SearchOrder", imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                //set parameters
                if (oCriteria.IsImported == null)
                {
                    command.Parameters.AddWithValue("@isImport", 2);
                }
                else
                {
                    command.Parameters.AddWithValue("@isImport", oCriteria.IsImported);
                }

                if (oCriteria.IsDeleted == null)
                {
                    command.Parameters.AddWithValue("@isDelete", 2);
                }
                else
                {
                    command.Parameters.AddWithValue("@isDelete", oCriteria.IsDeleted);
                }

                if (oCriteria.IsMaterial == null)
                {
                    command.Parameters.AddWithValue("@isMaterial", 2);
                }
                else
                {
                    command.Parameters.AddWithValue("@isMaterial", oCriteria.IsMaterial);
                }

                if (oCriteria.OrderCode != null)
                {
                    command.Parameters.AddWithValue("@orderCode", oCriteria.OrderCode);
                }
                if (oCriteria.EmployeeA != null)
                {
                    command.Parameters.AddWithValue("@IsAllEmployee", oCriteria.EmployeeA.EmployeeID);
                    command.Parameters.AddWithValue("@employeeAID", oCriteria.EmployeeA.EmployeeID);
                }
                else
                {
                    command.Parameters.AddWithValue("@IsAllEmployee", -1);
                }
                if (oCriteria.ExportedOrImportedDateFrom != null)
                {
                    command.Parameters.AddWithValue("@fromDate", oCriteria.ExportedOrImportedDateFrom);
                }
                if (oCriteria.ExportedOrImportedDateTo != null)
                {
                    command.Parameters.AddWithValue("@toDate", oCriteria.ExportedOrImportedDateTo);
                }
                command.Parameters.AddWithValue("@IsOrderCodeAll", oCriteria.IsAllOrderCode);

                var listOrder = new List<Order>();
                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        var employeeA = new Employee();
                        if (reader["EmployeeAID"].GetType().Equals(typeof (int)))
                        {
                            employeeA.EmployeeID = Convert.ToInt32(reader["EmployeeAID"]);
                            if (reader["EmployeeAName"].GetType().Equals(typeof (string)))
                            {
                                employeeA.FullName = Convert.ToString(reader["EmployeeAName"]);
                            }
                        }

                        var employeeB = new Employee();
                        if (reader["EmployeeBID"].GetType().Equals(typeof (int)))
                        {
                            employeeB.EmployeeID = Convert.ToInt32(reader["EmployeeBID"]);
                            if (reader["EmployeeBName"].GetType().Equals(typeof (string)))
                            {
                                employeeB.FullName = Convert.ToString(reader["EmployeeBName"]);
                            }
                        }
                        var contact = new Contact();
                        if (reader["ContactID"].GetType().Equals(typeof (int)))
                        {
                            contact.ContactID = Convert.ToInt32(reader["ContactID"]);
                            if (reader["ContactName"].GetType().Equals(typeof (string)))
                            {
                                contact.FullName = Convert.ToString(reader["ContactName"]);
                            }
                        }

                        //this code to do for material order
                        var newOrder = new Order();
                        if (reader["OrderID"].GetType().Equals(typeof (int)))
                        {
                            newOrder.OrderID = Convert.ToInt32(reader["OrderID"]);
                        }
                        newOrder.IsDeleted = Convert.ToBoolean(reader["IsDelete"]);
                        newOrder.OrderDate = Convert.ToDateTime(reader["OrderDate"]);
                        newOrder.OrderCode = Convert.ToString(reader["OrderCode"]);
                        newOrder.IsImported = Convert.ToBoolean(reader["IsImport"]);
                        newOrder.EmployeeA = employeeA;
                        newOrder.EmployeeB = employeeB;
                        newOrder.Contact = contact;
                        newOrder.MaterialCollection = null;
                        newOrder.ProductCollection = null;

                        listOrder.Add(newOrder);
                    }
                    feedback.Message = "Tìm kiếm thành công.";
                    feedback.IsSuccessful = true;
                    feedback.Data = listOrder;
                }
                else
                {
                    feedback.Message = "Không tồn tại bản ghi phù hợp.";
                    feedback.IsSuccessful = true;
                }

                //Log.Info("SearchEmployee: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                //Log.Error(e.Message);
                feedback.Message = "Unable to find";
            }
            catch (Exception t)
            {
                //Log.Error(t.Message);
                feedback.Message = "Unable to find";
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback UndoDelete(int objID)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region commentblock

        //public CollectionFeedback<Order> GetOrderCollectionByParameter(bool? isMaterial)
        //{
        //    if (isMaterial != null)
        //    {
        //        if (isMaterial == true)
        //        {
        //            return getAllOrder("GetCollectionMaterialOrder");
        //            //CollectionFeedback<Order> myFeedBack = new CollectionFeedback<Order>();
        //            //try
        //            //{
        //            //    SqlCommand command = new SqlCommand("GetCollectionMaterialOrder", imsConnection);
        //            //    command.CommandType = CommandType.StoredProcedure;
        //            //    DataTable dt = new DataTable("AllOrder");
        //            //    SqlDataAdapter sqlda = new SqlDataAdapter(command);
        //            //    sqlda.Fill(dt);
        //            //    List<Order> listOrder = new List<Order>();

        //            //    if (dt.Rows.Count != 0)
        //            //    {
        //            //        foreach (DataRow dataRow in dt.Rows)
        //            //        {

        //            //            int orderId = Convert.ToInt32(dataRow["OrderID"]);
        //            //            string orderCode = Convert.ToString(dataRow["OrderCode"]);
        //            //            DateTime orderDate = Convert.ToDateTime(dataRow["OrderDate"]);
        //            //            int employeeAid = Convert.ToInt32(dataRow["EmployeeAID"]);
        //            //            int contactId = Convert.ToInt32(dataRow["ContactID"]);
        //            //            int employeeBid = Convert.ToInt32(dataRow["EmployeeBID"]);
        //            //            //Boolean isImport = Convert.ToBoolean(dataRow["isImport"]);


        //            //            //this is material order
        //            //            //this code to do for material order
        //            //            Order newOrder = new Order();
        //            //            newOrder.OrderID = orderId;
        //            //            newOrder.OrderDate = orderDate;
        //            //            newOrder.OrderCode = orderCode;
        //            //            newOrder.IsImported = true;

        //            //            #region EmployeeA


        //            //            if (employeeAid != 0)
        //            //            {
        //            //                SqlCommand employeeCommand = new SqlCommand("GetSingleEmployee", imsConnection);
        //            //                employeeCommand.CommandType = CommandType.StoredProcedure;
        //            //                employeeCommand.Parameters.AddWithValue("@EmployeeID", employeeAid);
        //            //                OpenConnection();

        //            //                SqlDataReader reader = employeeCommand.ExecuteReader();

        //            //                if (reader.HasRows)
        //            //                {
        //            //                    while (reader.Read())
        //            //                    {

        //            //                        Employee newEmployee = new Employee();

        //            //                        newEmployee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
        //            //                        newEmployee.FullName = Convert.ToString(reader["FullName"]);
        //            //                        newEmployee.DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]);
        //            //                        newEmployee.IdentityCardNumber = Convert.ToString(reader["IdentityCardNumber"]);
        //            //                        newEmployee.Email = Convert.ToString(reader["Email"]);
        //            //                        newEmployee.Gender = Convert.ToString(reader["Gender"]);
        //            //                        newEmployee.JoinedDate = Convert.ToDateTime(reader["JoinedDate"]);
        //            //                        newEmployee.Address = Convert.ToString(reader["Address"]);
        //            //                        newEmployee.PhoneNumber = Convert.ToString(reader["PhoneNumber"]);
        //            //                        newEmployee.MobileNumber = Convert.ToString(reader["MobileNumber"]);
        //            //                        newEmployee.Description = Convert.ToString(reader["Description"]);

        //            //                        //create a new employmentType
        //            //                        EmploymentType newEmploymentType = new EmploymentType();
        //            //                        newEmploymentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
        //            //                        newEmploymentType.EmploymentTypeName = Convert.ToString(reader["[EmploymentType]"]);

        //            //                        //create a new role
        //            //                        Role newRole = new Role();
        //            //                        newRole.RoleID = Convert.ToInt32(reader["RoleID"]);
        //            //                        newRole.RoleName = Convert.ToString(reader["RoleName"]);

        //            //                        newEmployee.EmploymentType = newEmploymentType;
        //            //                        newEmployee.Role = newRole;

        //            //                        newOrder.EmployeeA = newEmployee;
        //            //                        break;
        //            //                    }

        //            //                }
        //            //                else
        //            //                {
        //            //                    newOrder.EmployeeA = null;
        //            //                }
        //            //            }
        //            //            else
        //            //            {
        //            //                newOrder.EmployeeA = null;
        //            //            }

        //            //            #endregion
        //            //            //this block code use to set employeeB to neworder

        //            //            #region EmployeeB
        //            //            if (employeeBid != 0)
        //            //            {
        //            //                SqlCommand employeeCommand = new SqlCommand("GetSingleEmployee", imsConnection);
        //            //                employeeCommand.CommandType = CommandType.StoredProcedure;
        //            //                employeeCommand.Parameters.AddWithValue("@EmployeeID", employeeBid);
        //            //                OpenConnection();

        //            //                SqlDataReader reader = employeeCommand.ExecuteReader();

        //            //                if (reader.HasRows)
        //            //                {
        //            //                    while (reader.Read())
        //            //                    {

        //            //                        Employee newEmployee = new Employee();

        //            //                        newEmployee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
        //            //                        newEmployee.FullName = Convert.ToString(reader["FullName"]);
        //            //                        newEmployee.DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]);
        //            //                        newEmployee.IdentityCardNumber = Convert.ToString(reader["IdentityCardNumber"]);
        //            //                        newEmployee.Email = Convert.ToString(reader["Email"]);
        //            //                        newEmployee.Gender = Convert.ToString(reader["Gender"]);
        //            //                        newEmployee.JoinedDate = Convert.ToDateTime(reader["JoinedDate"]);
        //            //                        newEmployee.Address = Convert.ToString(reader["Address"]);
        //            //                        newEmployee.PhoneNumber = Convert.ToString(reader["PhoneNumber"]);
        //            //                        newEmployee.MobileNumber = Convert.ToString(reader["MobileNumber"]);
        //            //                        newEmployee.Description = Convert.ToString(reader["Description"]);

        //            //                        //create a new employmentType
        //            //                        EmploymentType newEmploymentType = new EmploymentType();
        //            //                        newEmploymentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
        //            //                        newEmploymentType.EmploymentTypeName = Convert.ToString(reader["[EmploymentType]"]);

        //            //                        //create a new role
        //            //                        Role newRole = new Role();
        //            //                        newRole.RoleID = Convert.ToInt32(reader["RoleID"]);
        //            //                        newRole.RoleName = Convert.ToString(reader["RoleName"]);

        //            //                        newEmployee.EmploymentType = newEmploymentType;
        //            //                        newEmployee.Role = newRole;

        //            //                        newOrder.EmployeeB = newEmployee;
        //            //                        break;
        //            //                    }

        //            //                }
        //            //                else
        //            //                {
        //            //                    newOrder.EmployeeB = null;
        //            //                }
        //            //            }
        //            //            else
        //            //            {
        //            //                newOrder.EmployeeB = null;
        //            //            }
        //            //            #endregion
        //            //            //this block code use to set contact
        //            //            #region Contact
        //            //            if (contactId != 0)
        //            //            {
        //            //                SqlCommand contactCommand = new SqlCommand("GetSingleContact", imsConnection);
        //            //                contactCommand.CommandType = CommandType.StoredProcedure;
        //            //                contactCommand.Parameters.AddWithValue("@ContactID", contactId);
        //            //                OpenConnection();

        //            //                SqlDataReader reader = contactCommand.ExecuteReader();

        //            //                if (reader.HasRows)
        //            //                {
        //            //                    while (reader.Read())
        //            //                    {
        //            //                        Contact newContact = new Contact();
        //            //                        newContact.ContactID = Convert.ToInt32(reader["ContactID"]);
        //            //                        newContact.FullName = Convert.ToString(reader["FullName"]);
        //            //                        newContact.MobilePhone = Convert.ToString(reader["MobilePhone"]);
        //            //                        newContact.PartnerID = 0;
        //            //                        newOrder.Contact = newContact;
        //            //                        break;

        //            //                    }

        //            //                }
        //            //                else
        //            //                {
        //            //                    newOrder.Contact = null;
        //            //                }
        //            //            }
        //            //            else
        //            //            {
        //            //                newOrder.Contact = null;
        //            //            }
        //            //            #endregion

        //            //           //this block code use to set material list
        //            //            #region Material List

        //            //                SqlCommand materialCommand = new SqlCommand("GetGroupOfMaterial", imsConnection);
        //            //                materialCommand.CommandType = CommandType.StoredProcedure;
        //            //                materialCommand.Parameters.AddWithValue("@orderID", orderId);
        //            //                OpenConnection();
        //            //                SqlDataReader myReader = materialCommand.ExecuteReader();

        //            //                if (myReader.HasRows)
        //            //                {
        //            //                    List<Material> lisMaterial = new List<Material>();
        //            //                    while (myReader.Read())
        //            //                    {
        //            //                        Material newMaterial = new Material();
        //            //                        newMaterial.MaterialID = Convert.ToInt32(myReader["MaterialID"]);
        //            //                        newMaterial.MaterialCode = Convert.ToString(myReader["MaterialCode"]);
        //            //                        newMaterial.MaterialName = Convert.ToString(myReader["MaterialName"]);
        //            //                        newMaterial.Description = Convert.ToString(myReader["Description"]);
        //            //                        newMaterial.Quantity = Convert.ToInt32(myReader["Quantity"]);
        //            //                        newMaterial.PricePerUnit = Convert.ToDouble(myReader["PricePerUnit"]);
        //            //                        lisMaterial.Add(newMaterial);
        //            //                    }
        //            //                    newOrder.MaterialCollection = lisMaterial;
        //            //                }
        //            //                else
        //            //                {
        //            //                    newOrder.MaterialCollection = null;
        //            //                }
        //            //                #endregion

        //            //            newOrder.ProductCollection = null;

        //            //            listOrder.Add(newOrder);

        //            //        }
        //            //        myFeedBack.Message = "Get collection order successfully!";
        //            //        myFeedBack.Data = listOrder;
        //            //        myFeedBack.IsSuccessful = true;

        //            //    }
        //            //    else
        //            //    {
        //            //        myFeedBack.Message = "Get collection order unsuccessfully because of do not have any row in database!";
        //            //        myFeedBack.Data = null;
        //            //        myFeedBack.IsSuccessful = false;
        //            //    }
        //            //    //Log.Info("Get All Order: {0}, {1}", myFeedBack.Message, DateTime.Now.ToString());


        //            //}
        //            //catch (SqlException e)
        //            //{
        //            //    myFeedBack.Message = "Get collection order unsuccessfully because of SqlException!";
        //            //    myFeedBack.Data = null;
        //            //    myFeedBack.IsSuccessful = false;

        //            //}
        //            //catch (Exception e)
        //            //{
        //            //    //Log.Error(e.Message);
        //            //    myFeedBack.Message = "Get collection order unsuccessfully because of Exception!";
        //            //    myFeedBack.Data = null;
        //            //    myFeedBack.IsSuccessful = false;
        //            //}
        //            //finally
        //            //{
        //            //    CloseConnection();
        //            //}
        //            //return myFeedBack;   
        //        }
        //        else
        //        {
        //            return getAllOrder("GetCollectionProductOrder");
        //            //CollectionFeedback<Order> myFeedBack = new CollectionFeedback<Order>();
        //            //try
        //            //{
        //            //    SqlCommand command = new SqlCommand("GetCollectionProductOrder", imsConnection);
        //            //    command.CommandType = CommandType.StoredProcedure;
        //            //    DataTable dt = new DataTable("AllOrder");
        //            //    SqlDataAdapter sqlda = new SqlDataAdapter(command);
        //            //    sqlda.Fill(dt);
        //            //    List<Order> listOrder = new List<Order>();

        //            //    if (dt.Rows.Count != 0)
        //            //    {
        //            //        foreach (DataRow dataRow in dt.Rows)
        //            //        {

        //            //            int orderId = Convert.ToInt32(dataRow["OrderID"]);
        //            //            string orderCode = Convert.ToString(dataRow["OrderCode"]);
        //            //            DateTime orderDate = Convert.ToDateTime(dataRow["OrderDate"]);
        //            //            int employeeAid = Convert.ToInt32(dataRow["EmployeeAID"]);
        //            //            int contactId = Convert.ToInt32(dataRow["ContactID"]);
        //            //            int employeeBid = Convert.ToInt32(dataRow["EmployeeBID"]);
        //            //            Boolean isImport = Convert.ToBoolean(dataRow["isImport"]);


        //            //            //this is material order
        //            //            //this code to do for material order
        //            //            Order newOrder = new Order();
        //            //            newOrder.OrderID = orderId;
        //            //            newOrder.OrderDate = orderDate;
        //            //            newOrder.OrderCode = orderCode;
        //            //            newOrder.IsImported = isImport;

        //            //            #region EmployeeA


        //            //            if (employeeAid != 0)
        //            //            {
        //            //                SqlCommand employeeCommand = new SqlCommand("GetSingleEmployee", imsConnection);
        //            //                employeeCommand.CommandType = CommandType.StoredProcedure;
        //            //                employeeCommand.Parameters.AddWithValue("@EmployeeID", employeeAid);
        //            //                OpenConnection();

        //            //                SqlDataReader reader = employeeCommand.ExecuteReader();

        //            //                if (reader.HasRows)
        //            //                {
        //            //                    while (reader.Read())
        //            //                    {

        //            //                        Employee newEmployee = new Employee();

        //            //                        newEmployee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
        //            //                        newEmployee.FullName = Convert.ToString(reader["FullName"]);
        //            //                        newEmployee.DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]);
        //            //                        newEmployee.IdentityCardNumber = Convert.ToString(reader["IdentityCardNumber"]);
        //            //                        newEmployee.Email = Convert.ToString(reader["Email"]);
        //            //                        newEmployee.Gender = Convert.ToString(reader["Gender"]);
        //            //                        newEmployee.JoinedDate = Convert.ToDateTime(reader["JoinedDate"]);
        //            //                        newEmployee.Address = Convert.ToString(reader["Address"]);
        //            //                        newEmployee.PhoneNumber = Convert.ToString(reader["PhoneNumber"]);
        //            //                        newEmployee.MobileNumber = Convert.ToString(reader["MobileNumber"]);
        //            //                        newEmployee.Description = Convert.ToString(reader["Description"]);

        //            //                        //create a new employmentType
        //            //                        EmploymentType newEmploymentType = new EmploymentType();
        //            //                        newEmploymentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
        //            //                        newEmploymentType.EmploymentTypeName = Convert.ToString(reader["[EmploymentType]"]);

        //            //                        //create a new role
        //            //                        Role newRole = new Role();
        //            //                        newRole.RoleID = Convert.ToInt32(reader["RoleID"]);
        //            //                        newRole.RoleName = Convert.ToString(reader["RoleName"]);

        //            //                        newEmployee.EmploymentType = newEmploymentType;
        //            //                        newEmployee.Role = newRole;

        //            //                        newOrder.EmployeeA = newEmployee;
        //            //                        break;
        //            //                    }

        //            //                }
        //            //                else
        //            //                {
        //            //                    newOrder.EmployeeA = null;
        //            //                }
        //            //            }
        //            //            else
        //            //            {
        //            //                newOrder.EmployeeA = null;
        //            //            }

        //            //            #endregion
        //            //            //this block code use to set employeeB to neworder

        //            //            #region EmployeeB
        //            //            if (employeeBid != 0)
        //            //            {
        //            //                SqlCommand employeeCommand = new SqlCommand("GetSingleEmployee", imsConnection);
        //            //                employeeCommand.CommandType = CommandType.StoredProcedure;
        //            //                employeeCommand.Parameters.AddWithValue("@EmployeeID", employeeBid);
        //            //                OpenConnection();

        //            //                SqlDataReader reader = employeeCommand.ExecuteReader();

        //            //                if (reader.HasRows)
        //            //                {
        //            //                    while (reader.Read())
        //            //                    {

        //            //                        Employee newEmployee = new Employee();

        //            //                        newEmployee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
        //            //                        newEmployee.FullName = Convert.ToString(reader["FullName"]);
        //            //                        newEmployee.DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]);
        //            //                        newEmployee.IdentityCardNumber = Convert.ToString(reader["IdentityCardNumber"]);
        //            //                        newEmployee.Email = Convert.ToString(reader["Email"]);
        //            //                        newEmployee.Gender = Convert.ToString(reader["Gender"]);
        //            //                        newEmployee.JoinedDate = Convert.ToDateTime(reader["JoinedDate"]);
        //            //                        newEmployee.Address = Convert.ToString(reader["Address"]);
        //            //                        newEmployee.PhoneNumber = Convert.ToString(reader["PhoneNumber"]);
        //            //                        newEmployee.MobileNumber = Convert.ToString(reader["MobileNumber"]);
        //            //                        newEmployee.Description = Convert.ToString(reader["Description"]);

        //            //                        //create a new employmentType
        //            //                        EmploymentType newEmploymentType = new EmploymentType();
        //            //                        newEmploymentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
        //            //                        newEmploymentType.EmploymentTypeName = Convert.ToString(reader["[EmploymentType]"]);

        //            //                        //create a new role
        //            //                        Role newRole = new Role();
        //            //                        newRole.RoleID = Convert.ToInt32(reader["RoleID"]);
        //            //                        newRole.RoleName = Convert.ToString(reader["RoleName"]);

        //            //                        newEmployee.EmploymentType = newEmploymentType;
        //            //                        newEmployee.Role = newRole;

        //            //                        newOrder.EmployeeB = newEmployee;
        //            //                        break;
        //            //                    }

        //            //                }
        //            //                else
        //            //                {
        //            //                    newOrder.EmployeeB = null;
        //            //                }
        //            //            }
        //            //            else
        //            //            {
        //            //                newOrder.EmployeeB = null;
        //            //            }
        //            //            #endregion
        //            //            //this block code use to set contact
        //            //            #region Contact
        //            //            if (contactId != 0)
        //            //            {
        //            //                SqlCommand contactCommand = new SqlCommand("GetSingleContact", imsConnection);
        //            //                contactCommand.CommandType = CommandType.StoredProcedure;
        //            //                contactCommand.Parameters.AddWithValue("@ContactID", contactId);
        //            //                OpenConnection();

        //            //                SqlDataReader reader = contactCommand.ExecuteReader();

        //            //                if (reader.HasRows)
        //            //                {
        //            //                    while (reader.Read())
        //            //                    {
        //            //                        Contact newContact = new Contact();
        //            //                        newContact.ContactID = Convert.ToInt32(reader["ContactID"]);
        //            //                        newContact.FullName = Convert.ToString(reader["FullName"]);
        //            //                        newContact.MobilePhone = Convert.ToString(reader["MobilePhone"]);
        //            //                        newContact.PartnerID = 0;
        //            //                        newOrder.Contact = newContact;
        //            //                        break;

        //            //                    }

        //            //                }
        //            //                else
        //            //                {
        //            //                    newOrder.Contact = null;
        //            //                }
        //            //            }
        //            //            else
        //            //            {
        //            //                newOrder.Contact = null;
        //            //            }
        //            //            #endregion


        //            //                //this block code use to set product list
        //            //            #region product List

        //            //                SqlCommand productCommand = new SqlCommand("GetGroupOfProduct", imsConnection);
        //            //                productCommand.CommandType = CommandType.StoredProcedure;
        //            //                productCommand.Parameters.AddWithValue("@orderID", orderId);
        //            //                OpenConnection();
        //            //                SqlDataReader myReader = productCommand.ExecuteReader();

        //            //                if (myReader.HasRows)
        //            //                {
        //            //                    List<Product> listProduct = new List<Product>();
        //            //                    while (myReader.Read())
        //            //                    {
        //            //                        Product newProduct = new Product();
        //            //                        newProduct.ProductID = Convert.ToInt32(myReader["ProductID"]);
        //            //                        newProduct.ProductCode = Convert.ToString(myReader["ProductCode"]);
        //            //                        newProduct.ProductName = Convert.ToString(myReader["ProductName"]);
        //            //                        newProduct.Description = Convert.ToString(myReader["Description"]);
        //            //                        newProduct.Quantity = Convert.ToInt32(myReader["Quantity"]);
        //            //                        newProduct.MaterialCollection = null;
        //            //                        newProduct.PricePerUnit = 0;
        //            //                        listProduct.Add(newProduct);
        //            //                    }
        //            //                    newOrder.ProductCollection = listProduct;
        //            //                }
        //            //                else
        //            //                {
        //            //                    newOrder.ProductCollection = null;
        //            //                }
        //            //                #endregion

        //            //            newOrder.MaterialCollection = null;
        //            //            listOrder.Add(newOrder);

        //            //        }
        //            //        myFeedBack.Message = "Get collection order successfully!";
        //            //        myFeedBack.Data = listOrder;
        //            //        myFeedBack.IsSuccessful = true;

        //            //    }
        //            //    else
        //            //    {
        //            //        myFeedBack.Message = "Get collection order unsuccessfully because of do not have any row in database!";
        //            //        myFeedBack.Data = null;
        //            //        myFeedBack.IsSuccessful = false;
        //            //    }
        //            //    //Log.Info("Get All Order: {0}, {1}", myFeedBack.Message, DateTime.Now.ToString());


        //            //}
        //            //catch (SqlException e)
        //            //{
        //            //    myFeedBack.Message = "Get collection order unsuccessfully because of SqlException!";
        //            //    myFeedBack.Data = null;
        //            //    myFeedBack.IsSuccessful = false;

        //            //}
        //            //catch (Exception e)
        //            //{
        //            //    //Log.Error(e.Message);
        //            //    myFeedBack.Message = "Get collection order unsuccessfully because of Exception!";
        //            //    myFeedBack.Data = null;
        //            //    myFeedBack.IsSuccessful = false;
        //            //}
        //            //finally
        //            //{
        //            //    CloseConnection();
        //            //}
        //            //return myFeedBack;
        //        }
        //    }
        //    else
        //    {
        //        return getAllOrder("GetCollectionOrder");
        //        //CollectionFeedback<Order> myFeedBack = new CollectionFeedback<Order>();
        //        //try
        //        //{
        //        //    SqlCommand command = new SqlCommand("GetCollectionOrder", imsConnection);
        //        //    command.CommandType = CommandType.StoredProcedure;
        //        //    DataTable dt = new DataTable("AllOrder");
        //        //    SqlDataAdapter sqlda = new SqlDataAdapter(command);
        //        //    sqlda.Fill(dt);
        //        //    List<Order> listOrder = new List<Order>();

        //        //    if (dt.Rows.Count != 0)
        //        //    {
        //        //        foreach (DataRow dataRow in dt.Rows)
        //        //        {

        //        //            int orderId = Convert.ToInt32(dataRow["OrderID"]);
        //        //            string orderCode = Convert.ToString(dataRow["OrderCode"]);
        //        //            DateTime orderDate = Convert.ToDateTime(dataRow["OrderDate"]);
        //        //            int employeeAid = Convert.ToInt32(dataRow["EmployeeAID"]);
        //        //            int contactId = Convert.ToInt32(dataRow["ContactID"]);
        //        //            int employeeBid = Convert.ToInt32(dataRow["EmployeeBID"]);
        //        //            Boolean isImport = Convert.ToBoolean(dataRow["isImport"]);


        //        //            //this is material order
        //        //            //this code to do for material order
        //        //            Order newOrder = new Order();
        //        //            newOrder.OrderID = orderId;
        //        //            newOrder.OrderDate = orderDate;
        //        //            newOrder.OrderCode = orderCode;
        //        //            newOrder.IsImported = isImport;

        //        //            #region EmployeeA


        //        //            if (employeeAid != 0)
        //        //            {
        //        //                SqlCommand employeeCommand = new SqlCommand("GetSingleEmployee", imsConnection);
        //        //                employeeCommand.CommandType = CommandType.StoredProcedure;
        //        //                employeeCommand.Parameters.AddWithValue("@EmployeeID", employeeAid);
        //        //                OpenConnection();

        //        //                SqlDataReader reader = employeeCommand.ExecuteReader();

        //        //                if (reader.HasRows)
        //        //                {
        //        //                    while (reader.Read())
        //        //                    {

        //        //                        Employee newEmployee = new Employee();

        //        //                        newEmployee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
        //        //                        newEmployee.FullName = Convert.ToString(reader["FullName"]);
        //        //                        newEmployee.DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]);
        //        //                        newEmployee.IdentityCardNumber = Convert.ToString(reader["IdentityCardNumber"]);
        //        //                        newEmployee.Email = Convert.ToString(reader["Email"]);
        //        //                        newEmployee.Gender = Convert.ToString(reader["Gender"]);
        //        //                        newEmployee.JoinedDate = Convert.ToDateTime(reader["JoinedDate"]);
        //        //                        newEmployee.Address = Convert.ToString(reader["Address"]);
        //        //                        newEmployee.PhoneNumber = Convert.ToString(reader["PhoneNumber"]);
        //        //                        newEmployee.MobileNumber = Convert.ToString(reader["MobileNumber"]);
        //        //                        newEmployee.Description = Convert.ToString(reader["Description"]);

        //        //                        //create a new employmentType
        //        //                        EmploymentType newEmploymentType = new EmploymentType();
        //        //                        newEmploymentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
        //        //                        newEmploymentType.EmploymentTypeName = Convert.ToString(reader["[EmploymentType]"]);

        //        //                        //create a new role
        //        //                        Role newRole = new Role();
        //        //                        newRole.RoleID = Convert.ToInt32(reader["RoleID"]);
        //        //                        newRole.RoleName = Convert.ToString(reader["RoleName"]);

        //        //                        newEmployee.EmploymentType = newEmploymentType;
        //        //                        newEmployee.Role = newRole;

        //        //                        newOrder.EmployeeA = newEmployee;
        //        //                        break;
        //        //                    }

        //        //                }
        //        //                else
        //        //                {
        //        //                    newOrder.EmployeeA = null;
        //        //                }
        //        //            }
        //        //            else
        //        //            {
        //        //                newOrder.EmployeeA = null;
        //        //            }

        //        //            #endregion
        //        //            //this block code use to set employeeB to neworder

        //        //            #region EmployeeB
        //        //            if (employeeBid != 0)
        //        //            {
        //        //                SqlCommand employeeCommand = new SqlCommand("GetSingleEmployee", imsConnection);
        //        //                employeeCommand.CommandType = CommandType.StoredProcedure;
        //        //                employeeCommand.Parameters.AddWithValue("@EmployeeID", employeeBid);
        //        //                OpenConnection();

        //        //                SqlDataReader reader = employeeCommand.ExecuteReader();

        //        //                if (reader.HasRows)
        //        //                {
        //        //                    while (reader.Read())
        //        //                    {

        //        //                        Employee newEmployee = new Employee();

        //        //                        newEmployee.EmployeeID = Convert.ToInt32(reader["EmployeeID"]);
        //        //                        newEmployee.FullName = Convert.ToString(reader["FullName"]);
        //        //                        newEmployee.DateOfBirth = Convert.ToDateTime(reader["DateOfBirth"]);
        //        //                        newEmployee.IdentityCardNumber = Convert.ToString(reader["IdentityCardNumber"]);
        //        //                        newEmployee.Email = Convert.ToString(reader["Email"]);
        //        //                        newEmployee.Gender = Convert.ToString(reader["Gender"]);
        //        //                        newEmployee.JoinedDate = Convert.ToDateTime(reader["JoinedDate"]);
        //        //                        newEmployee.Address = Convert.ToString(reader["Address"]);
        //        //                        newEmployee.PhoneNumber = Convert.ToString(reader["PhoneNumber"]);
        //        //                        newEmployee.MobileNumber = Convert.ToString(reader["MobileNumber"]);
        //        //                        newEmployee.Description = Convert.ToString(reader["Description"]);

        //        //                        //create a new employmentType
        //        //                        EmploymentType newEmploymentType = new EmploymentType();
        //        //                        newEmploymentType.EmploymentTypeID = Convert.ToInt32(reader["EmploymentTypeID"]);
        //        //                        newEmploymentType.EmploymentTypeName = Convert.ToString(reader["[EmploymentType]"]);

        //        //                        //create a new role
        //        //                        Role newRole = new Role();
        //        //                        newRole.RoleID = Convert.ToInt32(reader["RoleID"]);
        //        //                        newRole.RoleName = Convert.ToString(reader["RoleName"]);

        //        //                        newEmployee.EmploymentType = newEmploymentType;
        //        //                        newEmployee.Role = newRole;

        //        //                        newOrder.EmployeeB = newEmployee;
        //        //                        break;
        //        //                    }

        //        //                }
        //        //                else
        //        //                {
        //        //                    newOrder.EmployeeB = null;
        //        //                }
        //        //            }
        //        //            else
        //        //            {
        //        //                newOrder.EmployeeB = null;
        //        //            }
        //        //            #endregion
        //        //            //this block code use to set contact
        //        //            #region Contact
        //        //            if (contactId != 0)
        //        //            {
        //        //                SqlCommand contactCommand = new SqlCommand("GetSingleContact", imsConnection);
        //        //                contactCommand.CommandType = CommandType.StoredProcedure;
        //        //                contactCommand.Parameters.AddWithValue("@ContactID", contactId);
        //        //                OpenConnection();

        //        //                SqlDataReader reader = contactCommand.ExecuteReader();

        //        //                if (reader.HasRows)
        //        //                {
        //        //                    while (reader.Read())
        //        //                    {
        //        //                        Contact newContact = new Contact();
        //        //                        newContact.ContactID = Convert.ToInt32(reader["ContactID"]);
        //        //                        newContact.FullName = Convert.ToString(reader["FullName"]);
        //        //                        newContact.MobilePhone = Convert.ToString(reader["MobilePhone"]);
        //        //                        newContact.PartnerID = 0;
        //        //                        newOrder.Contact = newContact;
        //        //                        break;

        //        //                    }

        //        //                }
        //        //                else
        //        //                {
        //        //                    newOrder.Contact = null;
        //        //                }
        //        //            }
        //        //            else
        //        //            {
        //        //                newOrder.Contact = null;
        //        //            }
        //        //            #endregion

        //        //            if (orderCode.Contains("M"))
        //        //            {
        //        //                //this block code use to set material list
        //        //                #region Material List

        //        //                SqlCommand materialCommand = new SqlCommand("GetGroupOfMaterial", imsConnection);
        //        //                materialCommand.CommandType = CommandType.StoredProcedure;
        //        //                materialCommand.Parameters.AddWithValue("@orderID", orderId);
        //        //                OpenConnection();
        //        //                SqlDataReader myReader = materialCommand.ExecuteReader();

        //        //                if (myReader.HasRows)
        //        //                {
        //        //                    List<Material> lisMaterial = new List<Material>();
        //        //                    while (myReader.Read())
        //        //                    {
        //        //                        Material newMaterial = new Material();
        //        //                        newMaterial.MaterialID = Convert.ToInt32(myReader["MaterialID"]);
        //        //                        newMaterial.MaterialCode = Convert.ToString(myReader["MaterialCode"]);
        //        //                        newMaterial.MaterialName = Convert.ToString(myReader["MaterialName"]);
        //        //                        newMaterial.Description = Convert.ToString(myReader["Description"]);
        //        //                        newMaterial.Quantity = Convert.ToInt32(myReader["Quantity"]);
        //        //                        newMaterial.PricePerUnit = Convert.ToDouble(myReader["PricePerUnit"]);
        //        //                        lisMaterial.Add(newMaterial);
        //        //                    }
        //        //                    newOrder.MaterialCollection = lisMaterial;
        //        //                }
        //        //                else
        //        //                {
        //        //                    newOrder.MaterialCollection = null;
        //        //                }
        //        //                #endregion
        //        //            }
        //        //            else
        //        //            {
        //        //                //this block code use to set product list
        //        //                #region product List

        //        //                SqlCommand productCommand = new SqlCommand("GetGroupOfProduct", imsConnection);
        //        //                productCommand.CommandType = CommandType.StoredProcedure;
        //        //                productCommand.Parameters.AddWithValue("@orderID", orderId);
        //        //                OpenConnection();
        //        //                SqlDataReader myReader = productCommand.ExecuteReader();

        //        //                if (myReader.HasRows)
        //        //                {
        //        //                    List<Product> listProduct = new List<Product>();
        //        //                    while (myReader.Read())
        //        //                    {
        //        //                        Product newProduct = new Product();
        //        //                        newProduct.ProductID = Convert.ToInt32(myReader["ProductID"]);
        //        //                        newProduct.ProductCode = Convert.ToString(myReader["ProductCode"]);
        //        //                        newProduct.ProductName = Convert.ToString(myReader["ProductName"]);
        //        //                        newProduct.Description = Convert.ToString(myReader["Description"]);
        //        //                        newProduct.Quantity = Convert.ToInt32(myReader["Quantity"]);
        //        //                        newProduct.MaterialCollection = null;
        //        //                        newProduct.PricePerUnit = 0;
        //        //                        listProduct.Add(newProduct);
        //        //                    }
        //        //                    newOrder.ProductCollection = listProduct;
        //        //                }
        //        //                else
        //        //                {
        //        //                    newOrder.ProductCollection = null;
        //        //                }
        //        //                #endregion
        //        //            }


        //        //            listOrder.Add(newOrder);

        //        //        }
        //        //        myFeedBack.Message = "Get collection order successfully!";
        //        //        myFeedBack.Data = listOrder;
        //        //        myFeedBack.IsSuccessful = true;

        //        //    }
        //        //    else
        //        //    {
        //        //        myFeedBack.Message = "Get collection order unsuccessfully because of do not have any row in database!";
        //        //        myFeedBack.Data = null;
        //        //        myFeedBack.IsSuccessful = false;
        //        //    }
        //        //    //Log.Info("Get All Order: {0}, {1}", myFeedBack.Message, DateTime.Now.ToString());


        //        //}
        //        //catch (SqlException e)
        //        //{
        //        //    myFeedBack.Message = "Get collection order unsuccessfully because of SqlException!";
        //        //    myFeedBack.Data = null;
        //        //    myFeedBack.IsSuccessful = false;

        //        //}
        //        //catch (Exception e)
        //        //{
        //        //    //Log.Error(e.Message);
        //        //    myFeedBack.Message = "Get collection order unsuccessfully because of Exception!";
        //        //    myFeedBack.Data = null;
        //        //    myFeedBack.IsSuccessful = false;
        //        //}
        //        //finally
        //        //{
        //        //    CloseConnection();
        //        //}
        //        //return myFeedBack;
        //    }

        //}

        #endregion

        protected CollectionFeedback<Order> getAllOrder(string storedName, bool isShowAll, bool? isMaterial)
        {
            var myFeedBack = new CollectionFeedback<Order>();
            try
            {
                var command = new SqlCommand(storedName, imsConnection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@isShowAll", isShowAll);
                //check if isMaterial is NULL -> search all
                //if isMaterial is 1 -> search material or 0 -> search product
                if (isMaterial == null)
                {
                    command.Parameters.AddWithValue("@isMaterial", 2);
                }
                else
                {
                    command.Parameters.AddWithValue("@isMaterial", isMaterial);
                }

                var dt = new DataTable("AllOrder");
                var sqlda = new SqlDataAdapter(command);
                sqlda.Fill(dt);
                var listOrder = new List<Order>();

                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        var employeeA = new Employee();
                        if (dataRow["EmployeeAID"].GetType().Equals(typeof (int)))
                        {
                            employeeA.EmployeeID = Convert.ToInt32(dataRow["EmployeeAID"]);
                            if (dataRow["EmployeeAName"].GetType().Equals(typeof (string)))
                            {
                                employeeA.FullName = Convert.ToString(dataRow["EmployeeAName"]);
                            }
                        }

                        var employeeB = new Employee();
                        if (dataRow["EmployeeBID"].GetType().Equals(typeof (int)))
                        {
                            employeeB.EmployeeID = Convert.ToInt32(dataRow["EmployeeBID"]);
                            if (dataRow["EmployeeBName"].GetType().Equals(typeof (string)))
                            {
                                employeeB.FullName = Convert.ToString(dataRow["EmployeeBName"]);
                            }
                        }
                        var contact = new Contact();
                        if (dataRow["ContactID"].GetType().Equals(typeof (int)))
                        {
                            contact.ContactID = Convert.ToInt32(dataRow["ContactID"]);
                            if (dataRow["ContactName"].GetType().Equals(typeof (string)))
                            {
                                contact.FullName = Convert.ToString(dataRow["ContactName"]);
                            }
                        }
                        //this code to do for material order
                        var newOrder = new Order();
                        if (dataRow["OrderID"].GetType().Equals(typeof (int)))
                        {
                            newOrder.OrderID = Convert.ToInt32(dataRow["OrderID"]);
                        }

                        newOrder.OrderDate = Convert.ToDateTime(dataRow["OrderDate"]);
                        newOrder.OrderCode = Convert.ToString(dataRow["OrderCode"]);
                        newOrder.IsImported = Convert.ToBoolean(dataRow["IsImport"]);
                        if (dataRow["IsDelete"] != DBNull.Value)
                        {
                            newOrder.IsDeleted = Convert.ToBoolean(dataRow["IsDelete"]);
                        }
                        newOrder.EmployeeA = employeeA;
                        newOrder.EmployeeB = employeeB;
                        newOrder.Contact = contact;
                        newOrder.MaterialCollection = null;
                        newOrder.ProductCollection = null;

                        listOrder.Add(newOrder);
                    }
                    myFeedBack.Message = InventoryConstants.Messages.GetSuccessfully;
                    myFeedBack.Data = listOrder;
                    myFeedBack.IsSuccessful = true;
                }
                else
                {
                    myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                    myFeedBack.Data = null;
                    myFeedBack.IsSuccessful = false;
                }
                //Log.Info("Get All Order: {0}, {1}", myFeedBack.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                myFeedBack.Data = null;
                myFeedBack.IsSuccessful = false;
            }
            catch (Exception e)
            {
                //Log.Error(e.Message);
                myFeedBack.Message = InventoryConstants.Messages.GetFailed;
                myFeedBack.Data = null;
                myFeedBack.IsSuccessful = false;
            }
            finally
            {
                CloseConnection();
            }
            return myFeedBack;
        }
    }
}